package bll

import (
	"context"
	"github.com/LyricTian/gin-admin/v7/internal/app/model/gormx/model"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/errors"
	"github.com/LyricTian/gin-admin/v7/pkg/util/uuid"
	"github.com/google/wire"
)

// GroupNotificationSet 注入GroupNotification
var GroupNotificationSet = wire.NewSet(wire.Struct(new(GroupNotification), "*"))

// GroupNotification 群通知
type GroupNotification struct {
	GroupNotificationModel *model.GroupNotification
}

// Query 查询数据
func (a *GroupNotification) Query(ctx context.Context, params schema.GroupNotificationQueryParam, opts ...schema.GroupNotificationQueryOptions) (*schema.GroupNotificationQueryResult, error) {
	return a.GroupNotificationModel.Query(ctx, params, opts...)
}

// GetFirst 查询数据
func (a *GroupNotification) GetFirst(ctx context.Context, params schema.GroupNotificationQueryParam, opts ...schema.GroupNotificationQueryOptions) (*schema.GroupNotification, error) {
	item, err := a.GroupNotificationModel.GetFirst(ctx, params, opts...)
	if err != nil {
		return nil, err
	}
	if item == nil {
		return nil, errors.ErrNotFound
	}

	return item, nil
}

// Get 查询指定数据
func (a *GroupNotification) Get(ctx context.Context, id string, opts ...schema.GroupNotificationQueryOptions) (*schema.GroupNotification, error) {
	item, err := a.GroupNotificationModel.Get(ctx, id, opts...)
	if err != nil {
		return nil, err
	} else if item == nil {
		return nil, errors.ErrNotFound
	}

	return item, nil
}

// Create 创建数据
func (a *GroupNotification) Create(ctx context.Context, item schema.GroupNotification) (*schema.IDResult, error) {

	item.ID = uuid.NewID()
	err := a.GroupNotificationModel.Create(ctx, item)
	if err != nil {
		return nil, err
	}

	return schema.NewIDResult(item.ID), nil
}

// Update 更新数据
func (a *GroupNotification) Update(ctx context.Context, id string, item schema.GroupNotification) error {
	_, err := a.Get(ctx, id)
	if err != nil {
		return err
	}

	return a.GroupNotificationModel.Update(ctx, id, item)
}

// Delete 删除数据
func (a *GroupNotification) Delete(ctx context.Context, id string) error {
	item, err := a.GroupNotificationModel.Get(ctx, id)
	if err != nil {
		return err
	}
	if item == nil {
		return errors.ErrNotFound
	}

	return a.GroupNotificationModel.Delete(ctx, id)
}
